<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="社会信用代码" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入社会信用代码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="企业名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入企业名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="法定代表人" prop="representative">
        <el-input v-model="queryParams.representative" placeholder="请输入法定代表人" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="注册地址" prop="registeredAddress">
        <el-input v-model="queryParams.registeredAddress" placeholder="请输入注册地址" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="企业联系电话" prop="telephone">
        <el-input v-model="queryParams.telephone" placeholder="请输入企业联系电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="入驻状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择入驻状态">
          <el-option v-for="item in stateList" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="state">
        <el-select v-model="queryParams.auditState" placeholder="请选择入驻状态">
          <el-option v-for="item in auditStateList" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['kjy:enterprise:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate2"
          v-hasPermi="['kjy:enterprise:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['kjy:enterprise:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="enterpriseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="社会信用代码" align="center" prop="code" />
      <el-table-column label="企业名称" align="center" prop="name" />
      <el-table-column label="企业类型" align="center" prop="type" />
      <el-table-column label="法定代表人" align="center" prop="representative" />
      <el-table-column label="企业营业执照" align="center" prop="businessLicense">
        <template slot-scope="scope">
          <el-image v-if="scope.row.businessLicense !== null" style="width: 50px; height: 50px"
            :src="getFirstLicense(scope.row.businessLicense)"
            :preview-src-list="[getFirstLicense(scope.row.businessLicense)]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="注册资本" align="center" prop="registeredCapital" />
      <el-table-column label="注册地址" align="center" prop="registeredAddress" />
      <el-table-column label="联系电话" align="center" prop="telephone" />
      <el-table-column label="邮箱" align="center" prop="mail" />
      <el-table-column label="成立时间" align="center" prop="establishmentTime" width="180">
        <template slot-scope="scope">
          <span>{{
      parseTime(scope.row.establishmentTime, "{y}-{m}-{d}")
    }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditState">
        <template slot-scope="{ row }">
          <el-tag v-if="row.auditState === 0" type="warning">审核中</el-tag>
          <el-tag v-if="row.auditState === 1" type="success">已通过</el-tag>
          <el-tag v-if="row.auditState === -1" type="danger">未通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="高企认定状态" align="center" prop="hightechEnterprises">
        <template slot-scope="{ row }">
          <el-tag v-if="row.hightechEnterprises === '0'"> 审核中 </el-tag>
          <el-tag v-if="row.hightechEnterprises === '1'" type="success">
            已通过
          </el-tag>
          <el-tag v-if="row.hightechEnterprises === '-1'" type="danger">
            未通过
          </el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="其他材料" align="center" prop="otherMaterials" /> -->
      <el-table-column label="企业简介" align="center" prop="briefIntroduction">
        <template slot-scope="scope">
          <el-popover popper-class="my-popper" trigger="hover" placement="top">
            <div v-html="scope.row.briefIntroduction"></div>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">悬浮查看</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="是否入驻" align="center" prop="state">
        <template slot-scope="{ row }">
          <el-tag v-if="row.state === 0" type="warning">未入驻</el-tag>
          <el-tag v-else type="success">已入驻</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="经费" align="center" prop="funding" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['kjy:enterprise:edit']">审核</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['kjy:enterprise:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.current" :limit.sync="queryParams.size"
      @pagination="getList" />

    <!-- 添加或修改企业对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="审核状态" prop="auditState">
          <el-radio v-model="form.auditState" :label="1">通过</el-radio>
          <el-radio v-model="form.auditState" :label="-1">不通过</el-radio>
        </el-form-item>
        <el-form-item v-if="form.auditState === -1" label="审核不通过原因" prop="failedReason">
          <el-input v-model="form.auditRemark" placeholder="请输入审核不通过原因" />
        </el-form-item>
        <!-- <el-form-item label="经费" prop="funding">
          <el-input v-model="form.funding" placeholder="请输入经费" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="open2" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="社会信用代码" prop="code">
          <el-input v-model="form.code" placeholder="请输入社会信用代码" />
        </el-form-item>
        <el-form-item label="企业名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="法定代表人" prop="representative">
          <el-input v-model="form.representative" placeholder="请输入法定代表人" />
        </el-form-item>

        <el-form-item label="注册资本" prop="registeredCapital">
          <el-input v-model="form.registeredCapital" placeholder="请输入注册资本" />
        </el-form-item>
        <el-form-item label="注册地址
" prop="registeredAddress">
          <el-input v-model="form.registeredAddress" placeholder="请输入注册地址
" />
        </el-form-item>
        <el-form-item label="联系电话
" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入联系电话
" />
        </el-form-item>
        <el-form-item label="邮箱" prop="mail">
          <el-input v-model="form.mail" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="成立时间" prop="establishmentTime">
          <el-date-picker clearable v-model="form.establishmentTime" type="date" value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择成立时间">
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="高企认定状态：未认定、审核中、已通过、未通过" prop="hightechEnterprises">
          <el-input v-model="form.hightechEnterprises" placeholder="请输入高企认定状态：未认定、审核中、已通过、未通过" />
        </el-form-item> -->
        <!-- <el-form-item label="其他材料" prop="otherMaterials">
          <el-input v-model="form.otherMaterials" placeholder="请输入其他材料" />
        </el-form-item> -->
        <el-form-item label="企业简介" prop="briefIntroduction">
          <div style="border: 1px solid #ccc">
            <web-editor style="height: 250px; overflow-y: hidden" v-model="form.briefIntroduction" placeholder="请输入企业简介" />
          </div>
        </el-form-item>
        <el-form-item label="企业营业执照" prop="businessLicense">
          <image-upload v-model="form.businessLicense" />
        </el-form-item>
        <!-- <el-form-item label="0未入驻,1入驻" prop="state">
          <el-input v-model="form.state" placeholder="请输入0未入驻,1入驻" />
        </el-form-item> -->
        <!-- <el-form-item label="高新技术企业证书" prop="highTechEnterpriseCertificate">
          <image-upload v-model="form.highTechEnterpriseCertificate"/>
        </el-form-item>
        <el-form-item label="高新技术企业公示信息表" prop="highTechEnterpriseNotice">
          <file-upload v-model="form.highTechEnterpriseNotice"/>
        </el-form-item> -->

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="cancel2">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  listEnterprise,
  getEnterprise,
  delEnterprise,
  addEnterprise,
  updateEnterprise,
} from "@/api/system/enterprise";

export default {
  name: "Enterprise",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业表格数据
      enterpriseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        code: null,
        name: null,
        type: null,
        representative: null,
        businessLicense: null,
        registeredCapital: null,
        registeredAddress: null,
        telephone: null,
        mail: null,
        establishmentTime: null,
        hightechEnterprises: null,
        otherMaterials: null,
        briefIntroduction: null,
        state: null,
        highTechEnterpriseCertificate: null,
        highTechEnterpriseNotice: null,
        funding: null,
        auditState: null,
      },
      // 表单参数
      form: {
        id: undefined,
        code: '',
        name: '',
        type: '',
        representative: '',
        businessLicense: '',
        registeredCapital: '',
        registeredAddress: '',
        telephone: '',
        mail: '',
        establishmentTime: '',
        otherMaterials: '',
        briefIntroduction: '',
        auditState: undefined,
        auditRemark: '',
      },
      // 表单校验
      rules: {},
      stateList: [
        { value: null, label: "全部" },
        { value: 0, label: "未入驻" },
        { value: 1, label: "已入驻" },
      ],
      auditStateList: [
        { value: null, label: "全部" },
        { value: 0, label: "未认证" },
        { value: 1, label: "已通过" },
        { value: -1, label: "未通过" },
        { value: 2, label: "审核中" },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询企业列表 */
    getList() {
      this.loading = true;
      listEnterprise(this.queryParams).then((response) => {
        this.enterpriseList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },

    getFirstLicense(license) {
      if (license && typeof license === 'string') {
        const licenses = license.split(',');
        if (licenses.length > 0) {
          return licenses[0].trim();
        }
      }
      return license;
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancel2() {
      this.open2 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        name: null,
        type: null,
        representative: null,
        businessLicense: null,
        registeredCapital: null,
        registeredAddress: null,
        telephone: null,
        mail: null,
        establishmentTime: null,
        hightechEnterprises: null,
        otherMaterials: null,
        briefIntroduction: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        state: null,
        highTechEnterpriseCertificate: null,
        highTechEnterpriseNotice: null,
        funding: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.auditState = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open2 = true;
      this.title = "添加企业";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getEnterprise(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改企业";
      });
    },
    handleUpdate2(row) {
      this.reset();
      const id = row.id || this.ids;
      getEnterprise(id).then((response) => {
        this.form = response.data;
        this.open2 = true;
        this.title = "修改企业";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateEnterprise(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEnterprise(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }, submitForm2() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateEnterprise(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open2 = false;
              this.getList();
            });
          } else {
            addEnterprise(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open2 = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除企业编号为"' + ids + '"的数据项？')
        .then(function () {
          return delEnterprise(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
  },
};
</script>
